import 'package:flutter/material.dart';
import 'package:flutter_grammar/widget/myDialog.dart';
import 'package:fluttertoast/fluttertoast.dart';

class DialogPage extends StatefulWidget {
  const DialogPage({super.key});
  @override
  State<DialogPage> createState() => _DialogPageState();
}

class _DialogPageState extends State<DialogPage> {
  // alert弹框
  void _alertDialog() async {
    var result = await showDialog(
        barrierDismissible: false, //表示点击灰色背景的时候是否消失弹出框
        context: context,
        builder: (context) {
          return AlertDialog(
            title: const Text("提示信息!"),
            content: const Text("您确定要删除吗"),
            actions: [
              TextButton(
                  onPressed: () {
                    print("ok");
                    Navigator.of(context).pop("ok"); //点击按钮让AlertDialog消失
                  },
                  child: const Text("确定")),
              TextButton(
                  onPressed: () {
                    print("cancel");
                    Navigator.of(context).pop("取消");
                  },
                  child: const Text("取消"))
            ],
          );
        });

    print("-----------");
    print(result);
  }

  // 选择框
  void _simpleDialog() async {
    var result = await showDialog(
        // barrierDismissible: false, //表示点击灰色背景的时候是否消失弹出框
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: const Text("请选择语言"),
            children: [
              SimpleDialogOption(
                onPressed: () {
                  print("汉语");
                  Navigator.pop(context, "汉语");
                },
                child: const Text("汉语"),
              ),
              const Divider(),
              SimpleDialogOption(
                onPressed: () {
                  print("英语");
                  Navigator.pop(context, "英语");
                },
                child: const Text("英语"),
              ),
              const Divider(),
              SimpleDialogOption(
                onPressed: () {
                  print("日语");
                  Navigator.pop(context, "日语");
                },
                child: const Text("日语"),
              ),
            ],
          );
        });

    print("-----------");
    print(result);
  }

  // 底部弹出框
  void _modelBottomSheet() async {
    var result = await showModalBottomSheet(
        context: context,
        builder: (context) {
          return SizedBox(
            // height: 280,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                ListTile(
                  title: const Text("分享"),
                  onTap: () {
                    print("分享");
                    Navigator.of(context).pop("分享");
                  },
                ),
                const Divider(),
                ListTile(
                  title: const Text("收藏"),
                  onTap: () {
                    print("收藏");
                    Navigator.of(context).pop("收藏");
                  },
                ),
                const Divider(),
                ListTile(
                  title: const Text("取消"),
                  onTap: () {
                    print("取消");
                    Navigator.of(context).pop("取消");
                  },
                ),
              ],
            ),
          );
        });
    print(result);
  }

  //  插件轻提示
  void _toast() {
    Fluttertoast.showToast(
        msg: "This is Center Short Toast",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER, // 提示位置
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.blue,
        textColor: Colors.white,
        fontSize: 16.0);
  }

  // 自定义弹框
  void _myDialog() async {
    // 实现自定义dialog
    showDialog(
       barrierDismissible: false,
        context: context,
        builder: (context) {
          return MyDialog(
              title: '温馨提示',
              content: 'this is Content',
              onTap: () {
                Navigator.pop(context);
              });
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('dialog'),
          centerTitle: true,
        ),
        body: Center(
          child: Column(children: [
            ElevatedButton(
                onPressed: _alertDialog, child: Text('alert dialog')),
            Text('this is alert'),
            SizedBox(
              height: 20,
            ),
            ElevatedButton(
                onPressed: _simpleDialog, child: Text('simple dialog')),
            Text('this is simple'),
            SizedBox(
              height: 20,
            ),
            ElevatedButton(
                onPressed: _modelBottomSheet, child: Text('model dialog')),
            Text('this is model'),
            SizedBox(
              height: 20,
            ),
            ElevatedButton(onPressed: _toast, child: Text('toast dialog')),
            Text('this is toast'),
            SizedBox(
              height: 20,
            ),
            ElevatedButton(
                onPressed: _myDialog, child: Text('myDialog dialog')),
            Text('this is mydialog'),
          ]),
        ));
  }
}
